<template>
  <div class="screen_container" v-if="detail">
    <el-row>
      <el-col :offset="20">
         <img :src="url" style="margin-top:30px"/>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-button type="primary" @click="print" :disabled="isDisabled" :loading="isLoading">打印</el-button>
      </el-col>
        <el-col>
          <div>
              <el-tag
          style="margin-right:20px"
            v-for="(value,key,index) in typeAndNums"
            :key="index"
            effect="plain"
            type="success"
          >
            {{ key+' ' +value }}
          </el-tag>
          </div>

        </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">键 </el-col>
      <el-col :span="6"> 值 </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> id </el-col>
      <el-col :span="6">{{ detail.id }}</el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> sn </el-col>
      <el-col :span="6"> {{ detail.monitorSn }}</el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('sn')"></el-button>
      </el-col>
    </el-row>
     <el-row>
      <el-col :span="6"> 类型 </el-col>
      <el-col :span="6"> {{ detail.deviceTypeName }}</el-col>
      <el-col :span="6" :offset="3">

      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 品牌 </el-col>
      <el-col :span="6">
        <span :class="{'red':detail.monitorBrand  =='UNKNOWN'}">{{ detail.monitorBrand }}</span>
        </el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('品牌')"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 型号 </el-col>
      <el-col :span="6">
           <span :class="{'red':detail.monitorModel  =='UNKNOWN'}">{{detail.monitorModel }}</span>

        </el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('型号')"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 分辨率 </el-col>
      <el-col :span="6">{{ detail.monitorResolution }} </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 大小 </el-col>
      <el-col :span="6">{{ detail.monitorSize }} </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">用户 </el-col>
      <el-col :span="6">{{ detail.userName }} </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> poId </el-col>
      <el-col :span="6"> {{ detail.poId }}</el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('poId')"></el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="6"> 品质 </el-col>
      <el-col :span="6"> {{ detail.quality }}</el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('品质')"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 状态 </el-col>
      <el-col :span="6"> {{ detail.passed ? "PASS" : "FAIL" }} </el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('状态')"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 成色 </el-col>
      <el-col :span="6"> {{ detail.deviceCondition }} </el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('成色')"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 年份 </el-col>
      <el-col :span="6"> {{ detail.year }} </el-col>
      <el-col :span="6" :offset="3"> </el-col>
    </el-row>
    <el-row>
      <el-col :span="6"> 备注 </el-col>
      <el-col :span="6"> {{ detail.mark }} </el-col>
      <el-col :span="6" :offset="3">
        <el-button type="primary" icon="el-icon-edit" @click="open_dialogForm('备注')"></el-button>
        <el-button

            type="danger"
            icon="el-icon-delete"

            @click="deleteMark"
          ></el-button>
      </el-col>

    </el-row>
      <el-row>
      <el-col :span="6">
        <el-button type="primary" @click="print" :disabled="isDisabled" :loading="isLoading">打印</el-button>
      </el-col>
       <el-col>
          <div>
              <el-tag
          style="margin-right:20px"
            v-for="(value,key,index) in typeAndNums"
            :key="index"
            effect="plain"
            type="success"
          >
            {{ key+' ' +value }}
          </el-tag>
          </div>

        </el-col>
    </el-row>

    <el-dialog
      :title="'修改' + title"
      :visible.sync="dialogFormVisible"
      width="40%"
      @close="closeUpdateDialog"
    >

     <el-form label-width="80px" >
       <!-- 1sn  -->
        <el-form-item label="sn" v-if="title=='sn'">
          <el-input v-model="screenUpdate.monitorSn" autocomplete="off"></el-input>
        </el-form-item>
      <!-- 2品牌 -->
        <el-form-item label="品牌" v-if="title=='品牌' || title=='型号'">
          <el-select
            placeholder="请选择品牌"
            filterable
            v-model="screenUpdate.monitorBrand"
             @change="handleChangeBrand"
          >
            <el-option
              v-for="item in brands"
              :key="item.brand"
              :label="item.brand"
              :value="item.brand"
            >
            </el-option>
          </el-select>
        </el-form-item>
          <!-- 3型号 -->
        <el-form-item label="型号" v-if="title=='型号' || title=='品牌'">
          <el-select
            placeholder="请选择型号"
            filterable
            v-model="screenUpdate.monitorModel"
          >
            <el-option
              v-for="item in models"
              :key="item.model"
              :label="item.model"
              :value="item.model"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 4 poId -->
        <el-form-item label="poId" v-if="title=='poId'">
          <el-select
            placeholder="请选择poId"
            filterable
            v-model="screenUpdate.poId"
          >
           <el-option
            v-for="item in poS"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
          </el-select>
        </el-form-item>
          <!-- 5品质 -->
        <el-form-item label="品质" v-if="title=='品质'">
          <el-radio-group v-model="screenUpdate.quality">
            <el-radio label="A">A</el-radio>
            <el-radio label="B">B</el-radio>
            <el-radio label="C">C</el-radio>
            <el-radio label="D">D</el-radio>
          </el-radio-group>
        </el-form-item>
          <!-- 6状态 -->
           <el-form-item label="状态" v-if="title=='状态'">
          <el-radio-group v-model="screenUpdate.passed">
            <el-radio :label="true">PASS</el-radio>
            <el-radio :label="false">FAIL</el-radio>
          </el-radio-group>
        </el-form-item>

          <!-- 7成色 -->
        <el-form-item label="成色" v-if="title=='成色'">
          <el-radio-group v-model="screenUpdate.deviceCondition">
                 <el-radio label="报废">报废</el-radio>
            <el-radio label="七成新">七成新</el-radio>
            <el-radio label="八成新">八成新</el-radio>
            <el-radio label="九成新">九成新</el-radio>
            <el-radio label="九五新">九五新</el-radio>
            <el-radio label="全新">全新</el-radio>
          </el-radio-group>
        </el-form-item>

          <el-form-item v-if="title=='备注'">
          <el-checkbox-group size="small" v-model="markGroup">
            <el-checkbox
              :label="item"
              border
              v-for="(item, index) in marks"
              :key="index"
              >{{ item }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" v-if="title=='备注'">
          <el-input v-model="screenUpdate.mark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
     <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateScreen">确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
import {
  reqUpdateScreen, reqGetDeviceDetail, reqGetDevices,
  reqGetBrands, reqGetModels, reqGetCheckMarks, reqPrint, reqGetPos, reqGetTypeAndNumByPo
} from '../utils/screen'
import QRCode from 'qrcode'
export default {
  name: 'screen_container',
  props: {},
  data () {
    return {
      typeAndNums: null,
      isDisabled: false,
      isLoading: false,
      id: null,
      detail: null,
      title: 'sn',
      dialogFormVisible: false,
      screenUpdate: {
        id: null,
        poId: null,
        monitorSn: null,
        monitorBrand: null,
        monitorModel: null,
        quality: null,
        passed: null,
        deviceCondition: null,
        mark: null,
        deviceTypeId: null
      },
      poS: null,
      devices: null,
      brands: null,
      models: null,
      // 所有复选框
      marks: null,
      // 被选中复选框
      markGroup: [],
      url: ''
    }
  },
  methods: {
    // 根据po获取对应类型和数量
    async getTypeAndNumByPo () {
      if (!this.detail.poId) {
        this.$message.error('请选择PO')
        return
      }
      const res = await reqGetTypeAndNumByPo(this.detail.poId)
      if (res.data.code == 0) {
        this.typeAndNums = res.data.data
      } else {
        this.$message.error(res.data.msg)
      }
    },
    // 生成二维码
    generateCode () {
      const self = this
      QRCode.toDataURL(this.id, { errorCorrectionLevel: 'H', version: 2 },
        function (err, url) {
          if (!err) {
            self.url = url
          }
          console.log(err)
        }
      )
    },
    async print () {
      if (this.detail.monitorBrand == 'UNKNOWN' || this.detail.monitorModel == 'UNKNOWN') {
        this.$message.error('品牌或者型号不能为UNKNOWN,请先修改再打印')
        return
      }
      this.isDisabled = true
      this.isLoading = true
      const res = await reqPrint(this.detail.id, this.detail.userId)
      if (res.data.code == 0) {
        this.$message({
          message: '打印成功',
          type: 'success'
        })
      } else {
        this.$message.error(res.data.msg)
      }
      setTimeout(() => {
        this.isDisabled = false
        this.isLoading = false
      }, 3000)
    },
    // 删除备注
    deleteMark () {
      this.$confirm('此操作将删除备注, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = { id: this.screenUpdate.id, mark: '' }
          const res = await reqUpdateScreen(data)
          if (res.data.code == 0) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getDeviceDetail()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((err) => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改显示器
    async updateScreen () {
      let data = null
      if (this.title == 'sn') {
        if (!this.screenUpdate.monitorSn) {
          this.$message.error('请输入sn')
          return
        }
        data = { id: this.screenUpdate.id, monitorSn: this.screenUpdate.monitorSn }
      }
      if (this.title == '品牌' || this.title == '型号') {
        if (!this.screenUpdate.monitorBrand || !this.screenUpdate.monitorModel) {
          this.$message.error('品牌与型号必须都有值')
          return
        }
        data = {
          id: this.screenUpdate.id,
          monitorBrand: this.screenUpdate.monitorBrand,
          deviceTypeId: this.screenUpdate.deviceTypeId,
          monitorModel: this.screenUpdate.monitorModel
        }
      }
      if (this.title == 'poId') {
        if (!this.screenUpdate.poId) {
          this.$message.error('请选择poId')
          return
        }
        data = { id: this.screenUpdate.id, poId: this.screenUpdate.poId }
      }
      if (this.title == '品质') {
        if (!this.screenUpdate.quality) {
          this.$message.error('请选择品质')
          return
        }
        data = { id: this.screenUpdate.id, quality: this.screenUpdate.quality }
      }
      if (this.title == '状态') {
        if (this.screenUpdate.passed == null) {
          this.$message.error('请选择状态')
          return
        }
        data = { id: this.screenUpdate.id, passed: this.screenUpdate.passed }
      }
      if (this.title == '成色') {
        if (!this.screenUpdate.deviceCondition) {
          this.$message.error('请选择成色')
          return
        }
        data = { id: this.screenUpdate.id, deviceCondition: this.screenUpdate.deviceCondition }
      }
      if (this.title == '备注') {
        let mark = ''
        if (this.markGroup.length) {
          this.markGroup.forEach((item, index) => {
            mark += item
            if (index != this.markGroup.length - 1) {
              mark += ','
            }
          })
        }
        if (this.screenUpdate.mark) {
          mark += this.screenUpdate.mark
        }

        if (this.markGroup.length == 0 && !this.screenUpdate.mark) {
          this.$message({
            message: '请至少选择一项',
            type: 'warning'
          })
          return
        }
        data = { id: this.screenUpdate.id, mark: mark }
      }
      const res = await reqUpdateScreen(data)
      if (res.data.code == 0) {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.dialogFormVisible = false
        this.getDeviceDetail(this.id)
      } else {
        this.$message.error(res.data.msg)
      }
    },
    // 打开对话框
    open_dialogForm (item) {
      this.title = item
      this.dialogFormVisible = true
    },
    // 设置字段
    setItem () {
      this.screenUpdate.id = this.detail.id
      this.screenUpdate.poId = this.detail.poId
      this.screenUpdate.monitorSn = this.detail.monitorSn
      this.screenUpdate.monitorBrand = this.detail.monitorBrand
      this.screenUpdate.monitorModel = this.detail.monitorModel
      this.screenUpdate.poId = this.detail.poId
      this.screenUpdate.quality = this.detail.quality
      this.screenUpdate.passed = this.detail.passed
      // this.screenUpdate.mark = this.detail.mark;
      this.screenUpdate.mark = null
      this.screenUpdate.deviceTypeId = this.detail.deviceTypeId
      this.screenUpdate.deviceCondition = this.detail.deviceCondition
    },
    // 获取设备详情
    async getDeviceDetail () {
      const res = await reqGetDeviceDetail(this.id)
      if (res.data.code == 0) {
        this.detail = res.data.data
        this.setItem()
        this.getMarks(this.screenUpdate.deviceTypeId)
        this.getTypeAndNumByPo()
      }
    },
    // 监听修改对话框关闭
    closeUpdateDialog () {
      this.setItem()
      this.markGroup = []
    },
    // 获取品牌
    async getBrands () {
      // 显示器
      const id = this.devices.filter((item) => {
        return item.typeName == '显示器'
      })[0].typeId

      const res = await reqGetBrands(id)
      if (res.data.code == 0) {
        this.brands = res.data.data
      }
    },
    // 获取设备
    async getDevices () {
      const res = await reqGetDevices()
      if (res.data.code == 0) {
        this.devices = res.data.data
        this.getBrands()
      }
    },
    // 切换品牌获取型号
    async handleChangeBrand (val) {
      const type_id = this.devices.filter((item) => {
        return item.typeName == '显示器'
      })[0].typeId

      const res = await reqGetModels(type_id, val)
      if (res.data.code == 0) {
        this.models = res.data.data
        this.screenUpdate.monitorModel = null
      }
    },
    // 查看所有po
    async getAllPo () {
      const res = await reqGetPos({ poStatusList: ['25'] })
      this.poS = res.data.data
    },
    // 获取可选备注
    async getMarks (id) {
      const res = await reqGetCheckMarks(id)
      if (res.data.code == 0) {
        this.marks = res.data.data
      }
    }
  },
  computed: {},
  created () {
    this.id = this.$route.query.id
    this.generateCode()
    this.getAllPo()
    this.getDevices()
    this.getDeviceDetail()
  },
  mounted () {

  }
}
</script>

<style scoped lang='less'>
.screen_container{
  width: 1200px;
  margin:auto;
  padding-top: 20px;
}
 .red{
    color: red;
  }
.el-row {
  height: 50px;
  display: flex;
  align-items: center;
}

.el-row:nth-child(2n + 1) {
  background-color: #DCDFE6;
}
.el-row:hover{
  background-color: #67C23A;
}
.el-row:nth-child(1) {
  background-color: #fff;
  color: blue;
}

</style>
